﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Kugar.UI.WinForm
{
    [Designer(typeof(NumericUpDownExControlDesinger))]
    public partial class NumericUpDownEx : NumericUpDown
    {
        private NumericUpDown _MaxValueControl;
        private NumericUpDown _MinValueControl;

        public NumericUpDownEx()
        {
            InitializeComponent();
         
        }

        public NumericUpDown MinValueControl
        {
            set
            {
                if (value == this || value == _MinValueControl)
                {
                    return;
                }

                if (_MinValueControl != null)
                {
                    _MinValueControl.ValueChanged -= MinControlValueChanged;
                }

                _MinValueControl = value;

                if (value != null)
                {
                    value.ValueChanged += MinControlValueChanged;
                    
                    MinControlValueChanged(value,null);
                }

            }
            get
            {
                return _MinValueControl;
            }
        }

        public NumericUpDown MaxValueControl
        {
            set
            {
                if (value==this || value==_MaxValueControl)
                {
                    return;
                }

                if (_MaxValueControl!=null)
                {
                    _MaxValueControl.ValueChanged -= MaxControlValueChanged;
                }

                _MaxValueControl = value;

                if (value!=null)
                {
                    value.ValueChanged += MaxControlValueChanged;
                    MaxControlValueChanged(value, null);
                }
              
            }
            get
            {
                return _MaxValueControl;
            }
        }

        private void MaxControlValueChanged(object sender, EventArgs e)
        {
            if (_MaxValueControl==null)
            {
                return;
            }

            var temp = (NumericUpDown) sender;

            if (temp==null)
            {
                return;
            }

            if (this.Value>temp.Value)
            {
                this.Value = temp.Value;
            }

            this.Maximum = temp.Value;
        }

        private void MinControlValueChanged(object sender, EventArgs e)
        {
            if (_MinValueControl == null)
            {
                return;
            }

            var temp = (NumericUpDown)sender;

            if (temp == null)
            {
                return;
            }

            if (this.Value < temp.Value)
            {
                this.Value = temp.Value;
            }

            this.Minimum = temp.Value;
        }

    }

    public class IntegerNumericUpDownEx:NumericUpDownEx
    {
        public new int Maximum
        {
            get
            {
                return (int) base.Maximum;
            }
            set
            {
                base.Maximum = value;
            }
        }

        public new int Minimum
        {
            get
            {
                return (int)base.Minimum;
            }
            set
            {
                base.Minimum = value;
            }
        }

        public new int Value
        {
            set
            {
                base.Value = value;
            }
            get
            {
                return (int) base.Value;
            }
        }

        public new int Increment
        {
            get
            {
                return (int) base.Increment;
            }
            set
            {
                base.Increment = (int) value;
            }
        }

        public new int DecimalPlaces
        {
            set
            {
                base.DecimalPlaces = 0;
            }
            get
            {
                return base.DecimalPlaces;

            }
        }
    }

    public class ByteNumericUpDownEx : NumericUpDownEx
    {
        public new byte Maximum
        {
            get
            {
                return (byte)base.Maximum;
            }
            set
            {
                base.Maximum = value;
            }
        }

        public new byte Minimum
        {
            get
            {
                return (byte)base.Minimum;
            }
            set
            {
                base.Minimum = value;
            }
        }

        public new byte Value
        {
            set
            {
                base.Value = value;
            }
            get
            {
                return (byte)base.Value;
            }
        }

        public new byte Increment
        {
            get
            {
                return (byte)base.Increment;
            }
            set
            {
                base.Increment = (int)value;
            }
        }

        public new int DecimalPlaces
        {
            set
            {
                base.DecimalPlaces = 0;
            }
            get
            {
                return base.DecimalPlaces;

            }
        }
    }
}